package com.zhu.algorithm.sort;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Random;
import java.util.stream.Stream;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zhujiabao
 * @Date: 2021/12/23/17:18
 * @Description:生成随机数组
 */
public class RandomArray {

    //读取配置文件的数据对象，转化成int数组
    public int[] randomArray_1000000()  {
        Properties prop = new Properties();

        InputStream in = RandomArray.class.getClassLoader().getResourceAsStream(
                "test.properties");
        try {
            prop.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //直接输出prop对象
        //System.out.println("直接输出prop对象:"+prop);

        //获取randomArray的值
        String randomArray=prop.getProperty("randomArray");
        int[] randomIntArray = Stream.of(randomArray.split(",")).mapToInt(Integer::parseInt).toArray();

        return randomIntArray;
    }


    public int[] randomArray(int n){

        int[] arr = new int[n];

        for (int i=0; i<n; i++){
            arr[i] = i;
        }

        Random random = new Random();
        int temp = 0;
        int N = n;
        for (int i=0; i<N; i++){
            int a = random.nextInt(n);
            int b = random.nextInt(n);

            if (a!=b){
                temp = arr[a];
                arr[a] = arr[b];
                arr[b] = temp;
            }
        }

        return arr;
    }
}
